package org.hxzon.workeasy.components;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.BeginRender;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.SetupRender;
import org.hxzon.util.DateIterator;
import org.hxzon.util.DateUtil;
import org.hxzon.util.MultiValueMap;
import org.hxzon.workeasy.model.EasyJob;

public class JobMonthTable {

    @Parameter
    private List<EasyJob> jobs;
    @Parameter
    private Date[] range;
    private MultiValueMap<String, EasyJob> jobsMap = new MultiValueMap<String, EasyJob>(new TreeMap());

    @SetupRender
    void setupRender() {
        Date startDate = (range[0] == null) ? range[2] : range[0];
        Date endDate = (range[4] == null) ? range[3] : range[5];
        DateIterator it = new DateIterator(DateUtil.toCalendar(startDate), DateUtil.toCalendar(endDate));
        for (; it.hasNext();) {
            Calendar calendar = it.next();
            int month = calendar.get(Calendar.MONTH) + 1;
            int date = calendar.get(Calendar.DATE);
            jobsMap.addKey(getKey(month, date));
        }
        for (EasyJob job : jobs) {
            add(job);
        }
    }

    @BeginRender
    void beginRender(MarkupWriter writer) {
        writer.element("table");
        writer.element("th");
        writer.element("td");
        writer.write("星期日");
        writer.end();
        writer.element("td");
        writer.write("星期一");
        writer.end();
        writer.element("td");
        writer.write("星期二");
        writer.end();
        writer.element("td");
        writer.write("星期三");
        writer.end();
        writer.element("td");
        writer.write("星期四");
        writer.end();
        writer.element("td");
        writer.write("星期五");
        writer.end();
        writer.element("td");
        writer.write("星期六");
        writer.end();
        writer.end();//th
        writer.element("tr");
        int i = 0;
        for (Map.Entry<String, List<EasyJob>> entry : jobsMap.entrySet()) {
            writer.element("td");
            String date = entry.getKey();
            List<EasyJob> jobs = entry.getValue();
            if (jobs != null) {
                for (EasyJob job : jobs) {
                    writer.write(job.getJobTitle());
                    writer.writeRaw("<br />");
                }
            }
            writer.end();//td
            i++;
            if (i % 6 == 0) {
                writer.end();
                writer.element("tr");
            }
        }
        writer.end();//tr
        writer.end();//table
    }

    private void add(EasyJob job) {
        int month = job.getNotifyMonth();
        int date = job.getNotifyDate();
        if (date == -1) {
            for (Map.Entry<String, List<EasyJob>> entry : jobsMap.entrySet()) {
                entry.getValue().add(job);
            }
        } else {
            jobsMap.put(getKey(month, date), job);
        }
    }

    private String getKey(int month, int date) {
        return (month < 10) ? ("0" + month) : month + ":" + date;
    }
}
